﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ResourceReportBuilder.Common
{
    public static class Utils
    {
        private static List<string> _htmlTags = new List<string>(new string[] { "<!DOCTYPE>", "<a>", "<abbr>", "<acronym>", "<address>", "<applet>", "<area>", "<b>", "<base>", "<basefont>", "<bdo>", "<big>", "<blockquote>", "<body>", "<br>", "<button>", "<caption>", "<center>", "<cite>", "<code>", "<col>", "<colgroup>", "<dd>", "<del>", "<dfn>", "<dir>", "<div>", "<dl>", "<dt>", "<em>", "<fieldset>", "<font>", "<form>", "<frame>", "<frameset>", "<head>", "<h1> - <h6>", "<hr>", "<html>", "<i>", "<iframe>", "<img>" });

        public static bool ContainsTags(this string value)
        {
            foreach (string tag in _htmlTags)
            {
                if (value.Contains(tag))
                {
                    return true;
                }
            }
            return false;
        }


        public static string GetRelativePath(string rootPath, string absolutePath)
        {
            int idx = absolutePath.IndexOf(rootPath);
            if (idx != 0)
            {
                return null;
            }
            return absolutePath.Substring(idx + rootPath.Length);
        }

        public static string[] SplitPath(string path)
        {
            return path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string FormatBytes(long bytes, int precision = 2)
        {
            const int scale = 1024;
            string[] suffixes = new string[] { "EB", "PB", "TB", "GB", "MB", "KB", "B" };
            long max = (long)Math.Pow(scale, suffixes.Length - 1);

            foreach (string sufix in suffixes)
            {
                if (bytes > max)
                {
                    string format = string.Format("{{0:##.{0}}} {{1}}", "#".PadLeft(precision, '#'));
                    return string.Format(format, decimal.Divide(bytes, max), sufix);
                }

                max /= scale;
            }
            return "0 B";
        }

        public static string GetMaxWidthPath(string path, int width, Font font)
        {
            System.Drawing.Size sz = TextRenderer.MeasureText(path, font);
            if (sz.Width + 2 <= width)
            {
                return path;
            }
            string[] dirs = path.Split('\\');
            if (dirs.Length < 2)
            {
                return path;
            }
            string result = string.Empty;
            string oldResult = string.Empty;
            string rightVal = string.Empty;
            string leftVal = string.Empty;
            int separatorWidth = TextRenderer.MeasureText("...\\", font).Width + 2;
            for (int i = 0; i < dirs.Length / 2; i++)
            {
                oldResult = rightVal;
                rightVal = System.IO.Path.Combine(dirs.ToList().Reverse<string>().Where((item, index) => index <= i).Reverse<string>().ToArray());
                if (TextRenderer.MeasureText(rightVal, font).Width + separatorWidth >= width)
                {
                    return oldResult;
                }
                rightVal = System.IO.Path.Combine("...", rightVal);
                leftVal = string.IsNullOrWhiteSpace(leftVal) ? dirs[i] : string.Format("{0}\\{1}", leftVal, dirs[i]);
                oldResult = result;
                result = string.Format("{0}\\{1}", leftVal, rightVal);
                if (TextRenderer.MeasureText(result, font).Width + 2 >= width)
                {
                    return oldResult;
                }
            }
            return path;
        }
    }
}
